package com.zufangbao.earth.handler.impl;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;
import freemarker.template.TemplateException;
@Component("mailSenderHandler")
public class MailSenderHandler {
	@Autowired
	private JavaMailSender mailSender;
	
	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	
	private MimeMessageHelper mimeMessageHelper;
	
	@Value("#{mail['mail.default.from']}")
	private String from;
	
	@Value("#{mail['mail.default.to']}")
	private String to;
	
	@Value("#{mail['mail.templateName']}")
	private String templateName;
	
	private String subject;
	private String content;
	
	private boolean validate = false; 
	
	/**
	 * 发送模板邮件
	 *	添加附件
	 */
	public void sendTemplateWithFile(Map<String,Object> model){
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		try {
			String email_content = create_email_content(model);
			sent_email(model, mimeMessage, email_content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mailSender.send(mimeMessage);
	}
	private void sent_email(Map<String,Object> filePath, MimeMessage mimeMessage,
			String email_content) throws MessagingException {
		String[] toUsers = to.split(";");
		MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(
				mimeMessage, true, "UTF-8");
		mimeMessageHelper.setTo(toUsers); // 接收人
		mimeMessageHelper.setFrom(from); // 发送人,从配置文件中取得
		mimeMessageHelper.setSubject(this.getSubject());
		mimeMessageHelper.setText(email_content, true);
		email_add_file(filePath, mimeMessageHelper);
	}
	private void email_add_file(Map<String, Object> filePath,
			MimeMessageHelper mimeMessageHelper) throws MessagingException {
		String finance_excel_file_path = filePath.get("finance_excel_file_path").toString();
		email_attach_excel_file(mimeMessageHelper, finance_excel_file_path);
		String overDue_excel_file_path = filePath.get("overDue_excel_file_path").toString();
		email_attach_excel_file(mimeMessageHelper, overDue_excel_file_path);
		String upcoming_excel_file_path = filePath.get("upcoming_excel_file_path").toString();
		email_attach_excel_file(mimeMessageHelper, upcoming_excel_file_path);
	}
	private void email_attach_excel_file(MimeMessageHelper mimeMessageHelper,
			String finance_excel_file_path) throws MessagingException {
		if (StringUtils.isNotEmpty(finance_excel_file_path)){
			FileSystemResource file = new FileSystemResource(new File(finance_excel_file_path));
			mimeMessageHelper.addAttachment(file.getFilename(), file);
		}
	}
	public String create_email_content(Map<String, Object> model)
			throws IOException, TemplateException {
		Template template = freeMarkerConfigurer.getConfiguration()
				.getTemplate(templateName, "UTF-8");
		return FreeMarkerTemplateUtils.processTemplateIntoString(
				template, model);
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public boolean isValidate() {
		return validate;
	}
	public void setValidate(boolean validate) {
		this.validate = validate;
	}
	public MimeMessageHelper getMimeMessageHelper() {
		return mimeMessageHelper;
	}
	public void setMimeMessageHelper(MimeMessageHelper mimeMessageHelper) {
		this.mimeMessageHelper = mimeMessageHelper;
	}
	
}